<template>
	<view class="main flex flex-dir-c flex-ai-c">
		<view style="width: 100%; height: 600rpx" class="flex flex-dir-c flex-jc-c flex-ai-c">
			<view>
				<image style="width: 230rpx; height: 60rpx" src="/static/logo.png"></image>
			</view>
			<view style="font-size: 36rpx; font-weight: 400; color: #333333; margin-top: 37rpx">登陆奥莱小样售货机系统</view>
		</view>
		<view class="page-body">
			<!-- catchtap="login"-->
			<button @tap="login" @getphonenumber="formSubmit" open-type="getPhoneNumber" v-if="show"
				style="margin: 30rpx 0; width: 616rpx; height: 80rpx; background: #fcb527; border-radius: 40rpx; font-size: 32rpx; vertical-align: middle"
				type="primary" formType="submit">
				<view class="flex flex-ai-c flex-jc-c" style="color: #fff;line-height: 80rpx;">授权手机号登陆</view>
			</button>
			<button @tap="login" v-else
				style="margin: 30rpx 0; width: 616rpx; height: 80rpx; background: #fcb527; border-radius: 40rpx; font-size: 32rpx; vertical-align: middle"
				type="primary" formType="submit">
				<view class="flex flex-ai-c flex-jc-c" style="color: #fff;line-height: 80rpx;">授权手机号登陆</view>
			</button>
			<view class="font-24" style="color: #999999;text-align: center;">
				
				<view class="flex flex-jc-c flex-ai-c">
					<radio :checked="show" color="#FCB527" @click="changeRadio"></radio>
					<view>
						我已阅读并同意<text @click="skip" style="color: #FCB527;">《用户协议》</text>和<text @click="skip" style="color: #FCB527;">《隐私政策》</text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapActions
	} from 'vuex';
	import user from '@/api2/user.js';
	export default {
		data() {
			return {
				show: false,
				order_id: null,
				authPhoneStatus: false,
				status: true
			};
		},
		onLoad(op) {
			if (op.order_id) {
				this.order_id = op.order_id
			}
			this.authPhoneStatus = uni.getStorageSync('authPhone')
		},
		methods: {
			skip(){
				uni.navigateTo({
					url: '/pages/me/userRule'
				})
			},
			changeRadio(e){
				this.show = !this.show
			},
			...mapActions({
				setUserInfo: 'setUserInfo'
			}),
			showToast(msg, icon = 'none') {
				uni.showToast({
					title: msg,
					icon: icon
				});
			},

			//授权手机号
			async authPhone(detail) {
				const res = await user.getPhone({
					code: encodeURIComponent(detail.code)
				})
				if(res.code == 200) {
					uni.setStorageSync('authPhone', true)
				}
				uni.setStorageSync('userInfo',res.data.userInfo)
				this.setUserInfo(res.data.userInfo)
			},

			//登录
			login() {
				if(!this.show) return this.showToast('请勾选用户协议和隐私政策')
				let _this = this;
				uni.showLoading({
					title: '登录中...'
				});
				wx.login({
					success: (res) => {
						if (res.code) {
							uni.getSetting({
								success(res3) {
									if (!res3.authSetting['scope.userInfo']) {
										uni.authorize({
											scope: 'scope.userInfo',
											success() {
												
												//发起网络请求getUserProfile/getUserInfo
												wx.getUserInfo({
													success: function(res2) {
														_this.authLogin(res2
															.encryptedData,
															res2.iv, res.code);
													}
												});
											}
										});
									} else {
										wx.getUserInfo({
											desc: '获取用户信息',
											success: function(res2) {
												_this.authLogin(res2.encryptedData, res2
													.iv, res.code);
											},
											fail: function(err) {
											}
										});
									}
								}
							});
						} else {
							_this.showToast('授权失败');
						}
					}
				});
			},

			//授权
			formSubmit(e) {
				if(!this.show) return
				if (!e.detail.code) {
					return;
				}
				let _this = this;
				if(!this.status) return
				if (this.order_id) {
					uni.redirectTo({
						url: `/pages/order/pay?order_id=${this.order_id}`
					});
				} else {
					let pages = getCurrentPages()
					if(pages.length > 1){
						uni.navigateBack()
					}else {
						uni.redirectTo({
							url: '/pages/index/index?index=2'
						});
					}
					
				}
				this.authPhone(e.detail);
			},

			authLogin(encryptedData, iv, code) {
				let _this = this;
				user.login({
						code: encodeURI(code),
						encryptedData: encodeURI(encryptedData),
						iv: encodeURI(iv)
					})
					.then((res) => {
						if(res.code === 200) {
							uni.setStorageSync('token', res.data.token);
							_this.status = true
						}else {
							_this.status = false
							_this.showToast(res.data.msg);
						}
						
						uni.hideLoading()
						
					})
					.catch((err) => {
						_this.status = false
						_this.showToast('登录失败');
						uni.hideLoading();
					});
			}
		}
	};
</script>
<style scoped>
	page {
		height: 100vh;
		background: #fff;
	}
</style>
<style lang="scss" scoped>
	.main {
		height: 100%;
		box-sizing: border-box;
	}

	.btn_test {
		margin-top: 300rpx;
	}

	button {
		font-size: 32rpx;
		background: #fff;
		border-radius: 15rpx;
		border: 0;
	}
</style>